package com.jvtd.eyl.ui.main.home.read.txt;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jvtd.eyl.R;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.databinding.FragmentTxtBinding;
import com.jvtd.eyl.ui.main.home.read.ReadMvpView;
import com.jvtd.eyl.ui.main.home.read.ReadPresenter;
import com.jvtd.eyl.ui.main.home.read.base.ReadBaseFragment;
import com.jvtd.eyl.utils.TxtUtils;
import com.jvtd.rxjava.JvtdObserverSubscriber;
import com.jvtd.utils.FileUtils;
import com.jvtd.widget.viewPager.JvtdViewPager;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TxtFragment extends ReadBaseFragment implements ReadMvpView{
  @Inject
  ReadPresenter<ReadMvpView> mPresenter;
  private FragmentTxtBinding mBinding;
  private static final String SELECT_KEY = "SELECT_KEY";
  private BookBean mBookBean;

  public static TxtFragment newInstance(BookBean bookBean) {
    Bundle args = new Bundle();
    args.putParcelable(SELECT_KEY,bookBean);
    TxtFragment fragment = new TxtFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  protected void initInject() {
    getFragmentComponent().inject(this);
  }

  @Override
  protected View getLayout(LayoutInflater inflater, @Nullable ViewGroup container) {
    mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_txt, container, false);
    return mBinding.getRoot();
  }

  @Override
  public void onDestroyView() {
    mPresenter.onDetach();
    super.onDestroyView();
  }

  @Override
  protected void initViewAndData() {
    super.initViewAndData();
    mPresenter.onAttach(this);
    if (getArguments() != null) {
      mBookBean = getArguments().getParcelable(SELECT_KEY);
    }
    if (mBookBean == null)
    {
      showMessage(R.string.error_message);
      return;
    }

    loadFile(mPresenter,mBookBean);
  }

  /**
   * 初始化pdf界面
   *
   * @author Chenlei
   * created at 2018/11/15
   **/
  private void initTxtView()
  {
    loadTxt();
  }

  /**
   * 加载txt
   */
  @MainThread
  private void loadTxt(){
    Observable.fromCallable(() -> {
      List<String> mStrings = new ArrayList<>();
      try
      {
        mStrings = TxtUtils.getTxtString(FileUtils.getFilePath(mContext,mBookBean.getUrl(), String.format(".%s", mBookBean.getFileType())));
      }catch (Exception e){
        e.printStackTrace();
      }
      return mStrings;
    }).compose(upstream -> upstream.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()))
            .subscribe(new JvtdObserverSubscriber<List<String>>(this) {
              @Override
              public void onNext(List<String> mStrings) {
                super.onNext(mStrings);
                mBinding.spinKit.setVisibility(View.GONE);
                mBinding.recyclerView.setData(mStrings);
              }

              @Override
              public void onError(Throwable e) {
                super.onError(e);
                showMessage(R.string.loading_failed);
              }
            });
  }

  /**
   * 下载成功
   *
   * @author Chenlei
   * created at 2018/11/15
   **/
  @Override
  public void downloadFileSuccess() {
    super.downloadFileSuccess();
    initTxtView();
  }

  /**
   * 下载失败
   *
   * @author Chenlei
   * created at 2018/11/15
   **/
  @Override
  public void downloadFileFailed()
  {
    showMessage(R.string.download_error_tips);
  }
}
